package com.loeng.framework.websocket.config.sender;

import com.loeng.framework.mq.redis.config.LoengRedisMQConsumerAutoConfiguration;
import com.loeng.framework.mq.redis.core.RedisMQTemplate;
import com.loeng.framework.websocket.core.sender.redis.RedisWebSocketMessageConsumer;
import com.loeng.framework.websocket.core.sender.redis.RedisWebSocketMessageSender;
import com.loeng.framework.websocket.core.session.WebSocketSessionManager;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;

/**
 * 基于 Redis 实现的 WebSocket 消息发送者配置
 * @ AutoConfiguration(before = LoengRedisMQConsumerAutoConfiguration.class)
 *  *      LoengRedisMQConsumerAutoConfiguration自动化类之类用到了 本配置类的一个bean 要本类先初始化
 * @author loeng zhuo
 */
@AutoConfiguration(before = LoengRedisMQConsumerAutoConfiguration.class)
@ConditionalOnProperty(prefix = "loeng.websocket", name = "sender-type", havingValue = "redis")
public class RedisWebSocketMessageSenderConfiguration {

    @Bean
    public RedisWebSocketMessageSender redisWebSocketMessageSender(WebSocketSessionManager sessionManager,
                                                                   RedisMQTemplate redisMQTemplate) {
        return new RedisWebSocketMessageSender(sessionManager, redisMQTemplate);
    }

    @Bean
    public RedisWebSocketMessageConsumer redisWebSocketMessageConsumer(
            RedisWebSocketMessageSender redisWebSocketMessageSender) {
        return new RedisWebSocketMessageConsumer(redisWebSocketMessageSender);
    }

}
